<?php
/**
 * @package admin
 * @copyright Copyright 2003-2006 Zen Cart Development Team
 * @copyright Portions Copyright 2003 osCommerce
 * @license http://www.zen-cart.com/license/2_0.txt GNU Public License V2.0
 * @version $Id: categories.php 4809 2006-10-22 18:53:22Z ajeh $
 */

// $Id:image_uploader.php,v1.0 2007/10/08 Jay T (Jaycode) teguhwpurwanto@gmail.com$
require('includes/application_top.php');
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="includes/stylesheet.css">
<link rel="stylesheet" type="text/css" href="includes/styles_ajax_image_swapper.css">
<script language="javascript" src="includes/javascript/AJAX_image_swapper.js"></script>
</head>
<body>
<?php
if (isset($_FILES['smallImage'])) {
	$ftmp = $_FILES['smallImage']['tmp_name'];
	$oname = $_FILES['smallImage']['name'];
	$dir = $_POST['img_dir'];
	$fname_small = DIR_WS_IMAGES . $dir . $_FILES['smallImage']['name'];
	$fname = '../' . $fname_small;
	if(move_uploaded_file($ftmp, $fname)){
		$upload_small_done = 1;
	}
}
if (isset($_FILES['medImage'])) {
	$ftmp = $_FILES['medImage']['tmp_name'];
	$oname = $_FILES['medImage']['name'];
	$dir = $_POST['img_dir'];
	$fname_array = explode('.', $_FILES['smallImage']['name']);
	$fname_med = DIR_WS_IMAGES . $dir . $fname_array[0] . '_MED.' . $fname_array[1];
	$fname = '../' . $fname_med;
	if(move_uploaded_file($ftmp, $fname)){
		$upload_med_done = 1;
	}
}
if (isset($_FILES['lrgImage'])) {
	$ftmp = $_FILES['lrgImage']['tmp_name'];
	$oname = $_FILES['lrgImage']['name'];
	$dir = $_POST['img_dir'];
	$fname_array = explode('.', $_FILES['smallImage']['name']);
	$fname_lrg = DIR_WS_IMAGES . $dir . $fname_array[0] . '_LRG.' . $fname_array[1];
	$fname = '../' . $fname_lrg;
	if(move_uploaded_file($ftmp, $fname)){
		$upload_lrg_done = 1;
	}
}

if (isset($_POST['title'])) {
	$title = $_POST['title'];
}
else {
	$title = $fname_array[0];
}

if (isset($upload_small_done)) {
	$fname_array = explode('.', $_FILES['smallImage']['name']);
	$sqlStr = 'INSERT INTO ' . TABLE_PRODUCTS_ATTRIBUTES_IMAGES . ' 
			   (image_id, products_attributes_id, image_path, image_title, image_sort_order) 
			   VALUES ( NULL, ' . $_POST['cur_attribute_id'] . ', \'' . $fname_small . '\', 
			   \'' . $title . '\', 0)';
	$db->Execute($sqlStr);
?>
<script>
	var productID = window.parent.document.getElementById('productID').value;
	var attributeID = window.parent.document.getElementById('attributeID').value;
	var optionID = window.parent.document.getElementById('optionID').value;
	if (window.ActiveXObject) {
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadImagesFromIframe&product_id=" + productID + "&attribute_id=" + attributeID + "&option_id=" + optionID + "&id=" + Number(new Date), true);	
		xHRObject.onreadystatechange = getData;
	}
	else if (window.XMLHttpRequest) {
		xHRObject.onreadystatechange = getData;
		xHRObject.open("GET","AJAX_servers/AJAX_image_swapper_server.php?action=loadImagesFromIframe&product_id=" + productID + "&attribute_id=" + attributeID + "&option_id=" + optionID + "&id=" + Number(new Date), true);	
	}
	xHRObject.send(null);	
</script>
<?php
}

?>
<form name="iform" action="image_uploader.php" method="post" enctype="multipart/form-data">
	<br />
	<div id="imageBrowser">
		
		<script>
			//yeah I know its crazy, but I should have written attributeID instead of optionID from start... oh well...
			var attribute_id = window.parent.document.getElementById('optionID').value;
			document.write('<input id="cur_attribute_id" type="hidden" name="cur_attribute_id" value="' + attribute_id + '" />');
		</script>
		Small image (default image) <br />
		<input id="smallImage" type="file" name="smallImage" onchange="displayImageName();" />
		<br /><br />
		Medium image <br />
		<input id="medImage" type="file" name="medImage" />
		<br /><br />
		Large image <br />
		<input id="lrgImage" type="file" name="lrgImage" />
		<br /><br />
		Upload to directory
<?php
			$default_directory = '';
			$dir = @dir(DIR_FS_CATALOG_IMAGES);
			$dir_info[] = array('id' => '', 'text' => "Main Directory");
			while ($file = $dir->read()) {
				if (is_dir(DIR_FS_CATALOG_IMAGES . $file) && strtoupper($file) != 'CVS' && $file != "." && $file != "..") {
				$dir_info[] = array('id' => $file . '/', 'text' => $file);
				}
			}
			echo zen_draw_pull_down_menu('img_dir', $dir_info, $default_directory, 'id="img_dir"');
?>
		<br /><br />
		Image title: <input type="text" id="title" name="title" value="" />
		<br /><br />
		Stored image name: <span id="imageName">Please browse the image(s)</span><br />
		<input type="submit" name="uploadImage" value="Upload Image" />
<?php
		if (isset($upload_small_done)) {
			echo '<br />Small file uploaded to ' . $fname_small;
		}
		if (isset($upload_med_done)) {
			echo '<br />Medium file uploaded to ' . $fname_med;
		}
		if (isset($upload_lrg_done)) {
			echo '<br />Large file uploaded to ' . $fname_lrg;
		}
?>
	</div>
</form>
</body>
</html>